﻿using System;
using System.Text;
using estoque.Model;

namespace estoque.ViewModel
{
    public class TaskVM : WorkItemVM //, IComparable
    {
        private bool isRunning;
        private string shortInfo;

        internal Task Task { get; private set; }

        public Priority TaskPriority { get { return this.Task.TaskPriority; } set { this.Task.TaskPriority = value; InvokePropertyChanged("TaskPriority"); } }

        public TimeSpan ProcessingTime { get { return this.Task.ProcessingTime; } set { this.Task.ProcessingTime = value; InvokePropertyChanged("ProcessingTime"); } }

        public bool IsBug { get { return this.Task.IsBug; } set { this.Task.IsBug = value; InvokePropertyChanged("IsBug"); } }

        public bool IsRunning { get { return this.isRunning; } set { this.isRunning = value; InvokePropertyChanged("IsRunning"); } }

        public new string ShortInfo
        {
            get
            {
                if (base.IsDirty || string.IsNullOrWhiteSpace(this.shortInfo))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(base.ShortInfo);
                    if (this.TaskPriority != Priority.none)
                        sb.AppendLine("Priorität: " + this.TaskPriority);
                    if (this.ProcessingTime.TotalMilliseconds > 0)
                        sb.AppendLine("Angewendete Zeit (in min): " + this.ProcessingTime.TotalMinutes.ToString("N2"));
                    if (this.IsBug)
                        sb.AppendLine("ist als Fehler definiert");
                    if (this.IsRunning)
                        sb.AppendLine("Zeitmessung läuft");
                    this.shortInfo = sb.ToString();

                    if (this.shortInfo.EndsWith("\r\n"))
                    {
                        this.shortInfo = this.shortInfo.Remove(this.shortInfo.Length - 2);
                    }
                    base.IsDirty = false;
                }

                return this.shortInfo;
            }
        }

        public TaskVM(Task task)
            : base(task)
        {
            this.Task = task;
        }

        public TaskVM(string title)
        {
            this.Task = new Task { Title = title };
            base.WorkItem = this.Task;
        }

        //public int CompareTo(object obj)
        //{
        //    TaskVM task = obj as TaskVM;

        //    if (task != null)
        //    {
        //        int result = this.TaskPriority.CompareTo(task.TaskPriority);
        //        if (result == 0)
        //        {
        //            if (this.DueDate.HasValue && task.DueDate.HasValue)
        //            {
        //                result = this.DueDate.Value.CompareTo(task.DueDate.Value);
        //            }
        //            else
        //            {
        //                result = this.DueDate.HasValue ? -1 : 1;
        //            }
        //        }

        //        return result;
        //    }

        //    return -1;
        //}
    }
}